//
//  AY_ReadMenu.m
//  AiYue
//
//  Created by mac on 2023/10/18.
//

#import "AY_ReadMenu.h"
#import "AY_ReadChapterView.h"
#import "AY_ReadChapterCell.h"
#import "AY_BookVpPage.h"

#define AnimationDelay 0.3
#define kToolBarHeight (50+[UIView safeBottomBar])
#define kToolBottomHeight (YNUM(222)+GK_STATUSBAR_HEIGHT)

@interface AY_ReadMenu ()<SXReadTopBarDelegate,AY_ReadBottomToolDelegate,SXReadCatalogViewDelegate>
@property (nonatomic,strong) UIView *showView;
@property (nonatomic,strong) UIView *gesView;
@property (nonatomic,strong) UIView *freeTingView; // 免费试听
@property (nonatomic,strong) AY_ReadChapterView *catalogView;
@end

@implementation AY_ReadMenu

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.gesView];
        [self addSubview:self.topBar];
        [self addSubview:self.bottomTool];
        [self addSubview:self.catalogView];
//        [self addSubview:self.fanYeView];
//        [self addSubview:self.setView];
        [self addSubview:self.freeTingView];
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)setIsEdit:(NSString *)isEdit {
    self.freeTingView.hidden = YES;
}

#pragma mark - 目录
- (void)muluAction:(UIButton *)btn {
    if (self.showView && self.showView!=self.catalogView) {
        [self.catalogView show];
        self.showView.frame = CGRectMake(0, SCREEN_H, self.showView.width, self.showView.height);
        self.showView = self.catalogView;
    }else{
        [self.catalogView show];
        self.showView = self.catalogView;
    }
}

-(void)setCharpters:(NSArray<ReadChapterModel *> *)charpters
{
    _charpters = charpters;
    self.catalogView.charpters = charpters;
}

- (void)setUserInfoModel:(UserInfo *)userInfoModel {
    _userInfoModel = userInfoModel;
}

- (void)setBookModel:(BookModel *)bookModel {
    _bookModel = bookModel;
    self.catalogView.bookModel = bookModel;
    if (bookModel.shelfFlag.intValue == 1) {
        self.topBar.addBookSelfBtn.hidden = YES;
    }
}

-(void)ges:(UITapGestureRecognizer *)ges
{
    if ([self.delegate respondsToSelector:@selector(cancelShowMenu:)]) {
        [self.delegate cancelShowMenu:self];
    }
}

-(void)showInView:(UIView *)view
{
    self.frame = view.bounds;
    [view addSubview:self];
    [UIView animateWithDuration:0.4 animations:^{
        self.freeTingView.alpha = 1.0;
//        self.addBookSelfView.alpha = 1.0;
        self.bottomTool.frame = CGRectMake(0, self.height-kToolBottomHeight, self.width, kToolBottomHeight);
        self.topBar.frame = CGRectMake(0, 0, self.width, [UIView navigationBar]+[UIView statusBar]);
        
    }completion:^(BOOL finished) {
        
    }];
}

-(void)dismiss
{
    [UIView animateWithDuration:0.4 animations:^{
        self.freeTingView.alpha = 0;
//        self.addBookSelfView.alpha = 0;
//        self.setView.frame = CGRectMake(0, MainScreenHeight, MainScreenWidth, 320 + HOME_INDICATOR_HEIGHT);
//        self.fanYeView.frame = CGRectMake(0, MainScreenHeight, MainScreenWidth, 320 + HOME_INDICATOR_HEIGHT);
        self.bottomTool.frame = CGRectMake(0, SCREEN_H, self.width, kToolBottomHeight);
         CGFloat height = [UIView navigationBar]+[UIView statusBar];
        self.topBar.frame = CGRectMake(0, -height, SCREEN_W, height);
        if (self.showView) {
            self.showView.frame = CGRectMake(0, SCREEN_H, self.showView.width, self.showView.height);
        }
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)showInView:(UIView *)view complete:(void(^)(void))complete
{
    self.frame = view.bounds;
    [view addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.bottomTool.frame = CGRectMake(0, SCREEN_H - kToolBottomHeight, self.width, kToolBottomHeight);
        self.topBar.frame = CGRectMake(0, 0, self.width, [UIView navigationBar]+[UIView statusBar]);
    } completion:^(BOOL finished) {
        if (complete) {
            complete();
        }
    }];
}

-(void)dismissComplete:(void(^)(void))complete
{
    [UIView animateWithDuration:0.3 animations:^{
        self.bottomTool.frame = CGRectMake(0, SCREEN_H, self.width, kToolBottomHeight);
         CGFloat height = [UIView navigationBar]+[UIView statusBar];
        self.topBar.frame = CGRectMake(0, -height, SCREEN_W, height);
        if (self.showView) {
            self.showView.frame = CGRectMake(0, SCREEN_H, self.showView.width, self.showView.height);
        }
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (complete) {
            complete();
        }
    }];
}

#pragma mark -Delegate
-(void)backAction
{
    if ([self.delegate respondsToSelector:@selector(backAction)]) {
        [self.delegate backAction];
    }
}

- (void)addBookSelfAction:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(addBookSelfAction:)]) {
        [self.delegate addBookSelfAction:btn];
    }
}

- (void)huyanAction:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(huyanChange)]) {
        [self.delegate huyanChange];
    }
}

- (void)fontChange {
    if ([self.delegate respondsToSelector:@selector(fontChange)]) {
        [self.delegate fontChange];
    }
}

- (void)changePageType:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(changePageType:)]) {
        [self.delegate changePageType:btn];
    }
}

- (void)brightnessChange {
    if ([self.delegate respondsToSelector:@selector(brightnessChange)]) {
        [self.delegate brightnessChange];
    }
}

- (void)didSelectCharpterWithIndex:(ReadChapterModel *)readChapterModel andChapterIndex:(NSInteger)index {
    if ([self.delegate respondsToSelector:@selector(didSelectCharpterWithIndex:andChapterIndex:)]) {
        [self.delegate didSelectCharpterWithIndex:readChapterModel andChapterIndex:index];
    }
}

- (void)freeTingAction
{
    if ([self.userInfoModel.vipFlag isEqual:@"1"]) {
        if ([self.delegate respondsToSelector:@selector(cancelShowMenu:)]) {
            [self.delegate cancelShowMenu:self];
        }
        if ([self.delegate respondsToSelector:@selector(showAudioView)]) {
            [self.delegate showAudioView];
        }
    }else {
//        if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
//            if ([self.delegate respondsToSelector:@selector(cancelShowMenu:)]) {
//                [self.delegate cancelShowMenu:self];
//            }
//            if ([self.delegate respondsToSelector:@selector(showAudioView)]) {
//                [self.delegate showAudioView];
//            }
//        }else {
//            AY_BookVpPage * vp = [[AY_BookVpPage alloc] init];
//            vp.bookId = self.bookModel.bookId;
//            [[AYTool viewControllerWith:self].navigationController pushViewController:vp animated:YES];
//        }
        if ([self.delegate respondsToSelector:@selector(cancelShowMenu:)]) {
            [self.delegate cancelShowMenu:self];
        }
        if ([self.delegate respondsToSelector:@selector(showAudioView)]) {
            [self.delegate showAudioView];
        }
    }
}

#pragma mark - 颜色
- (void)colorAction:(UIButton *)btn {
    self.topBar.backgroundColor = COLOR([AYMMKV getStringForKey:ReadToolBackColor defaultValue:@"FEF6E9"]);
    [self.topBar.addBookSelfBtn setTitleColor:COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"]) forState:UIControlStateNormal];
    [self.topBar.addBookSelfBtn setImage:[AYTool isNight] ? YImage(@"AY_add_bookself_night") : YImage(@"AY_add_bookself") forState:UIControlStateNormal];
    [self.topBar.backBtn setImage:[AYTool isNight] ? YImage(@"AY_nav_pop_w") : YImage(@"AY_nav_pop") forState:UIControlStateNormal];
    [self.catalogView setColors];
    if ([self.delegate respondsToSelector:@selector(colorChange)]) {
        [self.delegate colorChange];
    }
}

- (void)setCurrentChapter:(NSInteger)currentChapter {
    self.catalogView.currentChapter = currentChapter;
}

-(AY_ReadChapterView *)catalogView
{
    if (!_catalogView) {
        _catalogView = [[AY_ReadChapterView alloc] init];
        _catalogView.delegate = self;
    }
    return _catalogView;
}

-(UIView *)gesView
{
    if (!_gesView) {
        _gesView = [[UIView alloc] init];
        _gesView.backgroundColor = [UIColor clearColor];
        [_gesView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ges:)]];
    }
    return _gesView;
}

- (AY_ReadTopBar *)topBar {
    if (!_topBar) {
        CGFloat height = [UIView navigationBar]+[UIView statusBar];
        _topBar = [[AY_ReadTopBar alloc] initWithFrame:CGRectMake(0, -height, SCREEN_W, height)];
        _topBar.delegate = self;
    }
    return _topBar;
}

- (AY_ReadBottomTool *)bottomTool {
    if (!_bottomTool) {
        _bottomTool = [[AY_ReadBottomTool alloc] initWithFrame:CGRectMake(0, SCREEN_H, SCREEN_W, kToolBottomHeight)];
        _bottomTool.delegate = self;
        _bottomTool.layer.masksToBounds = YES;
        _bottomTool.layer.cornerRadius = YNUM(13);
    }
    return _bottomTool;
}

- (UIView *)freeTingView
{
    if (!_freeTingView) {
        _freeTingView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_W - 65, SCREEN_H - 280 - 60, 50, 50)];
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectZero];
        imageV.left = 0;
        imageV.top = 0;
        imageV.width = 50;
        imageV.height = 50;
        imageV.image = [UIImage imageNamed:@"AY_audio"];
        [_freeTingView addSubview:imageV];
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectZero];
        lab.left = 0;
        lab.bottom = 40.5;
        lab.height = 18.5;
        lab.width = 50;
        lab.font = YFont(10);
        lab.textColor = COLOR(@"312F14");
        lab.text = @"免费试听";
        lab.layer.masksToBounds = YES;
        lab.layer.cornerRadius = 9;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.backgroundColor = [UIColor whiteColor];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [btn addTarget:self action:@selector(freeTingAction) forControlEvents:UIControlEventTouchUpInside];
        [_freeTingView addSubview:btn];
    }
    return _freeTingView;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.gesView.frame = self.bounds;
}
@end
